package org.itboys.tools;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.itboys.weixin.tools.WeixinSendUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 给微信推送信息的线程
 * @author weisky
 *
 * 2016年4月30日
 */
public class WeixinSendInfoThread {
	
	protected static Logger logger = LoggerFactory.getLogger(WeixinSendInfoThread.class);

	//创建一个定长线程池，池的大小根据CPU支持的进程数来分配
	private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); 
	
	/**
	 * 推送一条消息
	 * @param appId
	 * @param appSecret
	 * @param touserOpenId
	 * @param content
	 */
	public static void sendMessage(String appId,String appSecret,String touserOpenId,String content){
		fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				WeixinSendUtils.sendMessage(appId, appSecret, touserOpenId, content);
			}
		});
	}
	
	/**
	 * 推送一条消息 带有间隔
	 * @param appId
	 * @param appSecret
	 * @param touserOpenId
	 * @param content
	 * @param sleep 睡眠时间 毫秒
	 */
	public static void sendMessageSleep(String appId,String appSecret,String touserOpenId,String content, int sleep){
		fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(sleep);
				} catch (Exception e) {
					e.printStackTrace();
				}
				WeixinSendUtils.sendMessage(appId, appSecret, touserOpenId, content);
			}
		});
	}

}
